package es.uoc.mxpdroid.ihm;

import java.io.File;

import com.example.mxpdroid.R;

import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.support.v4.app.NavUtils;
import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;

public class SettingsActivity extends Activity {
	
	private double factorK;
	private int method; 
	
	public static final String K_FACTOR			= "kFactor";
	public static final String DIST_METHOD		= "distMethod";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_settings);
		// Show the Up button in the action bar.
		setupActionBar();
		
		//getting extra data from intent that started this current activity
		Intent sender = getIntent();
		factorK = sender.getDoubleExtra(K_FACTOR, 2.0);
		method = sender.getIntExtra(DIST_METHOD, 0);
		
		int[] ids = {R.id.radio0, R.id.radio1, R.id.radio2, R.id.radio3};
		
		RadioButton rb = (RadioButton)findViewById(ids[method]);
		rb.setChecked(true);
		
		EditText tmp = (EditText) this.findViewById(R.id.kfactorText);
		tmp.setText(Double.toString(factorK));
	}

	/**
	 * Set up the {@link android.app.ActionBar}, if the API is available.
	 */
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	private void setupActionBar() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			getActionBar().setDisplayHomeAsUpEnabled(true);
		}
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	
	/**
	 * 
	 * @param vv
	 */
	public void saveConfigClick(View vv){
		EditText txt = (EditText)this.findViewById(R.id.kfactorText);
		factorK = Double.parseDouble(txt.getText().toString());
		
		RadioGroup rg=(RadioGroup)findViewById(R.id.radioGroup1);
		int id = rg.getCheckedRadioButtonId();
		
		switch(id){
		case R.id.radio0:
			method = 0;
			break;
		case R.id.radio1:
			method = 1;
			break;
		case R.id.radio2:
			method = 2;
			break;
		case R.id.radio3:
			method = 3;
			break;
		case R.id.radio4:
			method = 4;
			break;
		}
		
		Intent intent = new Intent();
		intent.putExtra(SettingsActivity.K_FACTOR, factorK);
		intent.putExtra(SettingsActivity.DIST_METHOD, method);
		setResult(RESULT_OK, intent);
		finish();
	}

}
